package com.cn.ycy.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.net.URI;

//@Component
public class GetRequestUrlGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
  @Override
  public GatewayFilter apply(NameValueConfig config) {
    return new GatewayFilter() {
      @Override
      public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        URI newUri = UriComponentsBuilder.fromUri(exchange.getRequest().getURI())
                .replaceQuery("ycy=1*&1").build().toUri();

        ServerHttpRequest request = exchange.getRequest().mutate().uri(newUri)
                .build();
        return chain.filter(exchange.mutate().request(request).build());
      }
    };
  }
}
